/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseItemDetailsDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseItemDetailsDao : BaseDao
    {
        private string defaultorder;
        private static BaseItemDetailsDao instance = null;
        private static object locker = new object();

        public BaseItemDetailsDao()
        {
            this.defaultorder = string.Empty;
            base.CurrentTableName = BaseItemDetailsTable.TableName;
        }

        public BaseItemDetailsDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseItemDetailsDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseItemDetailsDao(string tableName)
        {
            this.defaultorder = string.Empty;
            base.CurrentTableName = tableName;
        }

        public BaseItemDetailsDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public BaseItemDetailsDao(IDbHelper dbHelper, BaseUserInfo userInfo, string tableName)
            : this(dbHelper, userInfo)
        {
            base.CurrentTableName = tableName;
        }

        public string Add(BaseItemDetailsEntity itemDetailsEntity, out string statusCode)
        {
            string str = string.Empty;
            if (!string.IsNullOrEmpty(itemDetailsEntity.ItemCode) && this.Exists(BaseItemDetailsTable.FieldItemCode, itemDetailsEntity.ItemCode))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            if (!string.IsNullOrEmpty(itemDetailsEntity.ItemName) && this.Exists(BaseItemDetailsTable.FieldItemName, itemDetailsEntity.ItemName))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            if (!string.IsNullOrEmpty(itemDetailsEntity.ItemValue) && this.Exists(BaseItemDetailsTable.FieldItemValue, itemDetailsEntity.ItemValue))
            {
                statusCode = StatusCode.ErrorValueExist.ToString();
                return str;
            }
            str = this.AddEntity(itemDetailsEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddEntity(BaseItemDetailsEntity itemDetailsEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseItemDetailsTable.TableName);
            itemDetailsEntity.SortCode = sequence;
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(base.CurrentTableName);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldID, sequence);
            this.SetEntity(sqlBuilder, itemDetailsEntity);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseItemDetailsTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseItemDetailsEntity itemDetailsEntity = new BaseItemDetailsEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseItemDetailsTable.FieldID, DataRowVersion.Original].ToString();
                    if ((id.Length > 0) && base.AllowDelete)
                    {
                        num += this.Delete(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseItemDetailsTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    itemDetailsEntity.GetFrom(row);
                    if (base.AllowEdit)
                    {
                        num += this.UpdateEntity(itemDetailsEntity);
                    }
                    else
                    {
                        num += this.SetProperty(base.ID, BaseItemDetailsTable.FieldSortCode, base.SortCode);
                    }
                }
                if (row.RowState == DataRowState.Added)
                {
                    this.GetFrom(row);
                    num += (this.AddEntity(itemDetailsEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public DataTable GetDTByPermission(string userID, string resourceCategory, string permissionCode)
        {
            string[] ids = new BaseResourcePermissionScopeDao(base.DbHelper, base.UserInfo).GetResourceScopeIDs(userID, resourceCategory, permissionCode);
            DataTable dT = this.GetDT(ids);
            dT.DefaultView.Sort = BaseItemDetailsTable.FieldSortCode;
            return dT;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseItemDetailsEntity itemDetailsEntity)
        {
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldParentID, itemDetailsEntity.ParentID);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldItemCode, itemDetailsEntity.ItemCode);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldItemName, itemDetailsEntity.ItemName);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldItemValue, itemDetailsEntity.ItemValue);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldEnabled, itemDetailsEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldIsInput, itemDetailsEntity.IsInput ? 1 : 0);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldSortCode, itemDetailsEntity.SortCode);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldDescription, itemDetailsEntity.Description);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldAllowDelete, itemDetailsEntity.AllowDelete ? 1 : 0);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldAllowEdit, itemDetailsEntity.AllowEdit ? 1 : 0);
        }

        public int Update(BaseItemDetailsEntity itemDetailsEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, base.CurrentTableName, itemDetailsEntity.ID, itemDetailsEntity.ModifyUserID, itemDetailsEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
            }
            if (this.Exists(BaseItemDetailsTable.FieldItemCode, itemDetailsEntity.ItemCode, itemDetailsEntity.ID))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            if (this.Exists(BaseItemDetailsTable.FieldItemName, itemDetailsEntity.ItemName, itemDetailsEntity.ID))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return num;
            }
            if (this.Exists(BaseItemDetailsTable.FieldItemValue, itemDetailsEntity.ItemValue, itemDetailsEntity.ID))
            {
                statusCode = StatusCode.ErrorValueExist.ToString();
                return num;
            }
            num = this.UpdateEntity(itemDetailsEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseItemDetailsEntity itemDetailsEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(base.CurrentTableName);
            this.SetEntity(sqlBuilder, itemDetailsEntity);
            sqlBuilder.SetValue(BaseItemDetailsTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseItemDetailsTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseItemDetailsTable.FieldID, itemDetailsEntity.ID);
            return sqlBuilder.EndUpdate();
        }

        public string DefaultOrder
        {
            get
            {
                if (this.defaultorder.Length == 0)
                {
                    this.defaultorder = BaseItemDetailsTable.FieldSortCode;
                }
                return this.defaultorder;
            }
            set
            {
                this.defaultorder = value;
            }
        }

        public static BaseItemDetailsDao Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new BaseItemDetailsDao();
                        }
                    }
                }
                return instance;
            }
        }
    }
}